package uebString;

import htw.sortedList.*;
import java.util.*;
import exceptions.*;

/**
 *
 *
 * @author Andreas Dier, Steffen Schuhmann
 * @version a.01
 */


public class SortedStringDList extends DList implements AbstractSortedList {
	
	public void add(String object) throws Exception {
		
		int compare = 0;
		
		if(object == null)
			throw new NullPointerException();
		if(get(size - 1).compareTo(object) < 0) {
			addLast(object);
		}
		if(getFirst() == null) {
			set(0, object);
		} else {
			for(int i = 0; i < size; i++) {
				compare = get(i).compareTo(object);
				
				if(compare == 0) {
					throw new AlreadyInListException();
				} else if(compare > 0){
					add(i - 1, object);	
				}
			}
		}
	}
	
	private DListElement entry(int index) {
		if(index < 0 || index >= size)
			throw new IndexOutOfBoundsException();
			
		DListElement le = first;
		int i = 0;
		while(i < index) {
			le = le.next;
			i++;
		}
		return le;
	}
	
	public String get(int index) {
		return entry(index).data;
	}
}